package com.PopFax;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class loginBox extends Activity implements View.OnClickListener,DialogInterface.OnClickListener,View.OnKeyListener{
    /** Called when the activity is first created. */
    
    
    EditText txtItem;
    Button btnAdd,logout;
    ListView listItems;
    
    ArrayList<String> toDoItems;
    ArrayAdapter<String> aa;
    
    String[] sendfax={"Kingfisher","Fosters","Pedigree","Augustus"};
    String[] sendsms={"Pint","Zingaroo","River2"};
    String[] inbox={"Tea","Coffee","Latte","Choco Hotee"};
    String[] contacts={"Teachers","BP","Hague","Vat69"};
    String[] outbox={"Virgin Marry","Pussy Foot","Dream of Jinnie"};
    String[] settings={"setting1","setting2"};
    
    
    
    
    
    
    String currentMenu;
    
    
    
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginbox);
        
        txtItem=(EditText)findViewById(R.id.txtItem);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        logout=(Button)findViewById(R.id.logout);
        listItems=(ListView)findViewById(R.id.listItems);
        
        btnAdd.setOnClickListener(this);
        logout.setOnClickListener(this);
        txtItem.setOnKeyListener(this);
        
        toDoItems=new ArrayList<String>();
        aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,toDoItems);
        listItems.setAdapter(aa);
    }
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	
    	super.onCreateOptionsMenu(menu);
    	MenuItem menuItem;
    	menuItem = menu.add("inbox");
    	menuItem.setIcon(R.drawable.inbox);
    	menuItem = menu.add("outbox");
    	menuItem.setIcon(R.drawable.outbox);
    	menuItem = menu.add("send fax");
    	menuItem.setIcon(R.drawable.sendfax);
    	menuItem = menu.add("send sms");
    	menuItem.setIcon(R.drawable.sendsms);
    	menuItem = menu.add("contacts");
    	menuItem.setIcon(R.drawable.contacts);
    	menuItem = menu.add("settings");
    	menuItem.setIcon(R.drawable.settings);
    	
    	return true;
    }
    
    private void displayPopup(String title,String[] items){
    	AlertDialog.Builder builder= new AlertDialog.Builder(this);
    	builder.setTitle(title);
    	builder.setItems(items, this);
    	
    	builder.show();
    }
    
    private void addItem(String item) {
	
	if(item.length()>0)
	{
		
		Toast.makeText(getApplicationContext(),item +" addded", Toast.LENGTH_SHORT).show();
		this.toDoItems.add(item);
		this.aa.notifyDataSetChanged();
		
		this.txtItem.setText("");
	}
}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==this.btnAdd){
			this.addItem(this.txtItem.getText().toString());
		}
		if(v==this.logout){
			SQLiteDatabase myDB= null; 
	 		  String TableName = "store"; 
	 		  
	 		  String Data = ""; 
	 		  
	 		  /* Create a Database. */ 
	 		   
	 		   myDB = this.openOrCreateDatabase("storage", MODE_PRIVATE, null); 
	 		  
	 		   /* Create a Table in the Database. */ 
	 		   myDB.execSQL("DROP TABLE " 
	 		     + TableName 
	 		    +";");
	 		  Intent mIntent = new Intent(v.getContext(),Bean.class);
	 	    	startActivity(mIntent);
		}
		
	}
	public void deleteItem(int itemId){
		// TODO Auto-generated method stub
		if(itemId>=0){
			String itemName=(String)listItems.getItemAtPosition(itemId);
			Toast.makeText(getApplicationContext(),itemName+" deleted",Toast.LENGTH_SHORT).show();
			this.toDoItems.remove(itemId);
			aa.notifyDataSetChanged();
		}
		
	}


	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(event.getAction()==KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_DPAD_CENTER);
		{
	//	this.addItem(this.txtItem.getText().toString());
		}
	return false;
}

	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected(item);
		if(item.hasSubMenu()==false){
			if(item.getTitle()=="inbox"){
				currentMenu="inbox";
				this.displayPopup("Hot Drinks",this.inbox);
			}
				if(item.getTitle()=="outbox"){
					currentMenu="outbox";
					this.displayPopup("Mocktails Menu",this.outbox);
				}
					if(item.getTitle()=="send fax"){
						currentMenu="sendfax";
						this.displayPopup("Chilled Beer",this.sendfax);
					}
						if(item.getTitle()=="send sms"){
							currentMenu="sendsms";
							this.displayPopup("Canned Juices Menu",this.sendsms);
						}
							
						if(item.getTitle()=="contacts"){
							currentMenu="contacts";
							this.displayPopup("Wine menu",this.contacts);
						}
						if(item.getTitle()=="settings"){
								currentMenu="settings";
								this.displayPopup("Wine menu",this.settings);
							}
							
//							if(item.getTitle()=="delete"){
//							int index=listItems.getSelectedItemPosition();
//							this.deleteItem(index);
//							}
							
							
		}
		return true;	
	}


	
	public void onClick(DialogInterface dialog, int item) {
		if(currentMenu == "inbox") this.addItem(inbox[item]);
		if(currentMenu == "sendsms") this.addItem(sendsms[item]);
		if(currentMenu == "settings") this.addItem(settings[item]);
		if(currentMenu == "outbox") this.addItem(outbox[item]);
		if(currentMenu == "sendfax") this.addItem(sendfax[item]);
		if(currentMenu == "contacts") this.addItem(contacts[item]);
		
	}
}

